<!DOCTYPE html>
<html debug="true">
  <head>
    <script type="text/javascript" src="../../../../../openlayers/lib/OpenLayers.js"></script>
    <script type="text/javascript" src="../../../../../ext/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="../../../../../ext/ext-all.js"></script>
    <script type="text/javascript" src="../../../../lib/GeoExt.js"></script>

    <script type="text/javascript">

        function test_initialize(t) {
            t.plan(3);
            var store = new GeoExt.data.LayerStore();

            var Rec = GeoExt.data.LayerRecord.create();
            var rec = new Rec({
                layer: new OpenLayers.Layer("foo")
            });
            store.add(rec);
            var legend = new GeoExt.LayerLegend({
                layerRecord: rec,
                renderTo: "legend"
            });
            t.ok(legend instanceof GeoExt.LayerLegend, "Instance created correctly");
            t.eq(legend.items.get(0).text, "foo", "Layer text set correctly");
            legend.destroy();
            store.remove(rec);
            
            rec = new Rec({
                layer: new OpenLayers.Layer("foo"),
                hideTitle: true
            });
            store.add(rec);
            legend = new GeoExt.LayerLegend({
                layerRecord: rec,
                renderTo: "legend"
            });
            t.eq(legend.items.get(0).text, "", "Layer text for records with hideTitle set to true is empty.");
            legend.destroy();
        }
        
        function test_update(t) {
            t.plan(3);
            var store = new GeoExt.data.LayerStore();
            
            var Rec = GeoExt.data.LayerRecord.create([{
                name: "hideTitle", type: "boolean"
            }]);
            var rec = new Rec({
                layer: new OpenLayers.Layer("foo")
            });
            store.add(rec);
            var legend = new GeoExt.LayerLegend({
                layerRecord: rec,
                renderTo: "legend"
            });
            rec.set("title", "bar");
            t.eq(legend.items.get(0).text, "bar", "Layer text updated correctly");
            legend.destroy();
            store.remove(rec);

            rec = new Rec({
                layer: new OpenLayers.Layer("foo"),
                hideTitle: true
            });
            store.add(rec);
            legend = new GeoExt.LayerLegend({
                layerRecord: rec,
                renderTo: "legend"
            });
            rec.set("title", "bar");
            t.eq(legend.items.get(0).text, "", "Layer text for record with hideTitle set to true still empty after update.")
            legend.destroy();
            store.remove(rec);

            rec = new Rec({
                layer: new OpenLayers.Layer("foo")
            });
            store.add(rec)
            legend = new GeoExt.LayerLegend({
                layerRecord: rec,
                legendTitle: "bar",
                renderTo: "legend"
            });
            t.eq(legend.items.get(0).text, "bar", "legendTitle can be used to override layer name")
            legend.destroy();

        }

        function test_getTypes(t) {
            t.plan(5);
            var Rec = GeoExt.data.LayerRecord.create([{
                name: "legendURL"
            }]);
            var rec = new Rec({
                layer: new OpenLayers.Layer(),
                legendURL: "foo"
            });
            t.eq(GeoExt.LayerLegend.getTypes(rec), ["gx_urllegend"], "a layer record with plain layer and legendURL is supported by gx_urllegend only.");
            rec = new Rec({
                layer: new OpenLayers.Layer.WMS(),
                legendURL: "foo"
            });
            t.eq(GeoExt.LayerLegend.getTypes(rec), ["gx_urllegend","gx_wmslegend"], "a layer record with WMS layer and legendURL is supported by gx_urllegend and gx_wmslegend.");
            t.eq(GeoExt.LayerLegend.getTypes(rec, ["gx_wmslegend"]), ["gx_wmslegend","gx_urllegend"], "a layer record with WMS layer and legendURL and gx_wmslegend as preferred type lists  gx_wmslegend first, then gx_urllegend.");
            rec = new Rec({
                layer: new OpenLayers.Layer.WMS()
            });
            t.eq(GeoExt.LayerLegend.getTypes(rec), ["gx_wmslegend"], "a layer record with wms layer is supported by gx_wmslegend only.");
            
            rec = new Rec({
                layer: new OpenLayers.Layer.Vector()
            });
            t.eq(GeoExt.LayerLegend.getTypes(rec), ["gx_vectorlegend"], "a layer record with vector layer is supported by gx_vectorlegend only.");
            
        }

    </script>
  <body>
    <div id="legend"></div>
  </body>
</html>